<?php

include('config.php');
$tbl_name = "categories";
$FileName = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME); //dit geeft de naam van de pagina terug, zonder .php erachter.


$sql = mysql_query("select * FROM $tbl_name") or die(mysql_error());

//echo "<table border='3' width='550px;'><tr><td>subcat1</td><td>subcat2</td><td>subcat3</td></tr>";
$hoofdcat = " "; //telkens kijken we of de hoofdcategorie hetzelfde is of niet
$subcat = " "; //same shit, maar dan voor subcat
$lg=1;
$niv=0;

echo"<ul id='treemenu1' class='treeview'>";$niv++;
while($r = mysql_fetch_array($sql))

{

$subcat1 = $r['SubCategory1'];
$subcat2 = $r['SubCategory2'];
$subcat3= $r['SubCategory3']; 
if(!$subcat2 || $subcat2=='') { 
	if($lg==3){$niv--;$niv--;echo"</ul></ul>";}
	if($lg==2){$niv--;echo"</ul>";}
	echo "</li><li>$niv$subcat1";
	$lg=1; 
	

	}	
elseif(!$subcat3 || $subcat3=='') {
	if($subcat1 == $hoofdcat)
	{
		
		if($lg==3){echo"</ul></li>";$niv--;;}
		echo"<li>$niv$subcat2";
		$subcat=$subcat2;
		$lg=2;

		
	}
	else{
		if($lg==2){echo"</ul>";$niv--;}
		if($lg==3){echo"</ul></ul>";$niv--;$niv--;}
		echo"</li><li>$niv$subcat1";$niv++;echo" <ul><li>$niv$subcat2</li>";
		$hoofdcat = $subcat1;
		$lg=2;

	}
}
else
{
	if($subcat1 == $hoofdcat)
	{
		if($subcat2 == $subcat)
		{
			if($lg==2){echo"<ul>";$niv++;}//??????
			echo"<li>$niv$subcat3</li>";
			$lg=3;
		}
		else{
		$niv++;if($lg==1)echo"<ul>";
		if($lg==2){echo"</ul>";	$niv--;}
		if($lg==3){echo"</ul></ul>";$niv--;$niv--;}
		;echo"<li>$niv$subcat2<li>";$niv++;echo" <ul><li>$niv $subcat3</li>";
		$subcat = $subcat2;
		$lg=3;


		
		}
		
	}
	else{
		if($lg==2){echo"</ul>";$niv--;}
		if($lg==3){echo"</ul></ul>";$niv--;$niv--;}
		$niv++;echo"</li><li>$niv$subcat1<ul><li>$niv $subcat2</li>";$niv++;echo"<ul><li>$niv $subcat3</li>";
		$hoofdcat = $subcat1;
		$subcat = $subcat2;
		$lg=3;
		
	}
}

}

//close up the table
echo "</ul>";

?>